﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace CourseSelectionSystem
{
    public partial class AdminView : Form
    {
        private CourseSelectionSystemServices service;
        public AdminView()
        {
            InitializeComponent();
            service = new CourseSelectionSystemServices();

            //获取当前时间
            label1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

            //启用计时器
            timer1.Start();
        }

        private void AdminView_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 设置获取当前实时时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
        }

        private void toolStripStatusLabel1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 查看学生信息列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 学生列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var lst = service.GetAlls();
            this.dataGridView1.DataSource = lst;
        }

        /// <summary>
        /// 查看教师信息列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 教师列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var lst = service.GetAllt();
            this.dataGridView1.DataSource = lst;
        }

        /// <summary>
        /// 查看课程信息列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 课程列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var lst = service.GetAllc();
            this.dataGridView1.DataSource = lst;
        }

        /// <summary>
        /// 新增课程信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 新增课程信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddCourse course = new AddCourse();
            var result = course.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAllc();
                this.dataGridView1.DataSource = lst;
            }
        }

        /// <summary>
        /// 新增学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 新增学生ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddStudent student = new AddStudent();
            var result = student.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAlls();
                this.dataGridView1.DataSource = lst;
            }
        }

        /// <summary>
        /// 新增课程信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 新增教师信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddTeacher teacher = new AddTeacher();
            var result = teacher.ShowDialog();
            if (result == DialogResult.OK)
            {
                var lst = service.GetAllt();
                this.dataGridView1.DataSource = lst;
            }
        }

        /// <summary>
        /// 修改课程信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 修改课程信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.dataGridView1.CurrentRow != null)
            {
                string ID = this.dataGridView1.CurrentRow.Cells["ID"].Value?.ToString();
                EditCourse course = new EditCourse(ID);
                var result = course.ShowDialog();
                if (result == DialogResult.OK && course.SaveChanges())
                {
                    var lst = service.GetAllc();
                    this.dataGridView1.DataSource = lst;
                    var cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    cm.Refresh();
                }
            }
        }

        /// <summary>
        /// 修改教师信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 修改教师信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                string ID = this.dataGridView1.CurrentRow.Cells["ID"].Value?.ToString();
                EditTeacher teacher = new EditTeacher(ID);
                var result = teacher.ShowDialog();
                if (result == DialogResult.OK && teacher.SaveChanges())
                {
                    var lst = service.GetAllt();
                    this.dataGridView1.DataSource = lst;
                    var cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    cm.Refresh();
                }
            }
        }

        /// <summary>
        /// 修改学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 修改学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                string ID = this.dataGridView1.CurrentRow.Cells["ID"].Value?.ToString();
                EditStudent student = new EditStudent(ID);
                var result = student.ShowDialog();
                if (result == DialogResult.OK && student.SaveChanges())
                {
                    var lst = service.GetAlls();
                    this.dataGridView1.DataSource = lst;
                    var cm = (CurrencyManager)BindingContext[dataGridView1.DataSource];
                    cm.Refresh();
                }
            }
        }

        /// <summary>
        /// 删除学生信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(this.dataGridView1.CurrentRow != null)
            {
                string ID = this.dataGridView1.CurrentRow.Cells["ID"].Value?.ToString();
                string Name = (string)this.dataGridView1.CurrentRow.Cells["Name"].Value;

                //检查当前数据表是否为学生信息表
                if (CheckIfStudent())
                {
                    var result = MessageBox.Show($"是否删除学生【{Name}】的信息？", "系统提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        service.Deletes(ID);
                        var lst = service.GetAlls();
                        this.dataGridView1.DataSource = lst;
                    }
                }
                else
                {
                    MessageBox.Show("当前表不是学生信息表，请确认后再进行删除操作！", "错误提示");
                }
            }
        }
        /// <summary>
        /// 检查是否为学生信息表
        /// </summary>
        /// <returns></returns>
        private bool CheckIfStudent()
        {
            return this.dataGridView1.Columns.Contains("Class");
        }

        /// <summary>
        /// 删除教师信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除教师信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                string ID = this.dataGridView1.CurrentRow.Cells["ID"].Value?.ToString();
                string Name = (string)this.dataGridView1.CurrentRow.Cells["Name"].Value;

                if (CheckIfTeacher())
                {
                    var result = MessageBox.Show($"是否删除教师【{Name}】的信息？", "系统提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        service.Deletet(ID);
                        var lst = service.GetAllt();
                        this.dataGridView1.DataSource = lst;
                    }
                }
                else
                {
                    MessageBox.Show("当前表不是教师信息表，请确认后再进行删除操作！", "错误提示");
                }
            }
        }

        /// <summary>
        /// 检查是否为教师信息表
        /// </summary>
        /// <returns></returns>
        private bool CheckIfTeacher()
        {
            return this.dataGridView1.Columns.Contains("Title");
        }

        /// <summary>
        /// 删除课程信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除课程信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentRow != null)
            {
                string ID = this.dataGridView1.CurrentRow.Cells["ID"].Value?.ToString();
                string Name = (string)this.dataGridView1.CurrentRow.Cells["Name"].Value;
                if (CheckIfCourse())
                {
                    var result = MessageBox.Show($"是否删除课程【{Name}】的信息？", "系统提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        service.Deletec(ID);
                        var lst = service.GetAllc();
                        this.dataGridView1.DataSource = lst;
                    }
                }
                else
                {
                    MessageBox.Show("当前表不是课程信息表，请确认后再进行删除操作！", "错误提示");
                }
            }
        }

        /// <summary>
        /// 检查是否为课程信息表
        /// </summary>
        /// <returns></returns>
        private bool CheckIfCourse()
        {
            return this.dataGridView1.Columns.Contains("Credit");
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}
